<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/23
 * Time: 上午12:43
 */

namespace Home\Model;
use Think\Model;

class TrustModel extends Model{
    protected $tableName = 'trust';
    protected $pk         = 'id';

    //获取甲方个人主页信任圈子信息
    public function getTrusterInfo($trusted_id){
        $trust_field = array('truster_id');
        $trust_order = '`time` desc';
        $trust_map['trusted_id'] = array('eq',$trusted_id);
        $trust_result = $this->field($trust_field)->order($trust_order)->where($trust_map)->select();
        //dump($trust_result);
        if(is_array($trust_result)){
            $signal = 0;
            foreach($trust_result as &$trust_result_one){
                //判断本地用户是否信任该trusted_id
                if( $_SESSION['loginUser']['user_id'] == $trust_result_one['truster_id'] ){
                    $signal = 1;
                }

               // 在user表中获取具体信息
                $user_field = array('user_name','image_url');
                $user_map['user_id'] = array('eq',$trust_result_one['truster_id']);
                $user_db = M('User');
                $user_result = $user_db->field($user_field)->where($user_map)->select();
                if(is_array($user_result)){
                    $trust_result_one['user_name'] = $user_result[0]['user_name'];
                    $trust_result_one['image_url'] = $user_result[0]['image_url'];
                }else{
                    //user表获取信息失败
                }
            }
            if( 1 == $signal ){
                $trust_signal = 2;
            }else{
                $trust_signal = 1;
            }

            $trust_result_return['truster_arr']= $trust_result;
            $trust_result_return['is_trusted'] = $trust_signal;

            return $trust_result_return;
        }else{
            return false;
        }

    }
} 